package com.yupi.springbootinit.utils;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import static com.yupi.springbootinit.constant.MapConstant.*;

public class MapUtils {
    String origin = "114.41843,23.03843";
    String destination = "114.385689,23.076714";
    String address = "广东省惠州市惠城区演达大道46号";
    String postUrl = DIRECTION_API +"strategy=2&" + "origin=" + origin + "&destination=" + destination + "&key=" + MAP_KEY;
    String geoUrl = GEO_API + "address=" + address + "&key=" + MAP_KEY;

    public static String getDestination(String origin,String destination) {
        String postUrl = DIRECTION_API +"strategy=2&" + "origin=" + origin + "&destination=" + destination + "&key=" + MAP_KEY;
        try {
            System.out.println("通过路径规划获取距离测试：");
            String response = sendGetRequest(postUrl);
            Gson gson = new Gson();

            // 将JSON字符串转换为JsonObject
            JsonObject responseObject = gson.fromJson(response, JsonObject.class);

            // 获取"route"对象
            JsonObject routeObject = responseObject.getAsJsonObject("route");

            // 获取"paths"数组
            JsonArray pathsArray = routeObject.getAsJsonArray("paths");

            // 由于paths数组的索引从0开始，我们可以直接获取第一个元素
            JsonElement pathElement = pathsArray.get(0);

            // 将JsonElement转换为JsonObject以获取"distance"
            JsonObject pathObject = pathElement.getAsJsonObject();

            // 获取"distance"值
            String distance = pathObject.get("distance").getAsString();

            // 打印"distance"值
            System.out.println("Distance: " + distance);
            System.out.println(response);
            return distance;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static String getLocation(String address) {
        String geoUrl = GEO_API + "address=" + address + "&key=" + MAP_KEY;
        try {
            System.out.println("通过地理编码获取经纬度：");
            String response = sendGetRequest(geoUrl);
            Gson gson = new Gson();

            // 将JSON字符串转换为JsonObject
            JsonObject responseObject = gson.fromJson(response, JsonObject.class);


            // 获取"paths"数组
            JsonArray pathsArray = responseObject.getAsJsonArray("geocodes");

            // 由于paths数组的索引从0开始，我们可以直接获取第一个元素
            JsonElement pathElement = pathsArray.get(0);

            // 将JsonElement转换为JsonObject以获取"distance"
            JsonObject pathObject = pathElement.getAsJsonObject();

            // 获取"distance"值
            String location = pathObject.get("location").getAsString();

            // 打印"distance"值
            System.out.println("location: " + location);
            System.out.println(response);
            return location;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    private static String sendGetRequest(String url) throws IOException {
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("GET");

        int responseCode = con.getResponseCode();
        System.out.println("Sending GET request to URL: " + url);
        System.out.println("Response Code: " + responseCode);

        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuilder response = new StringBuilder();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        return response.toString();
    }
}
